package com.wunu.smartlock1.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wunu.smartlock1.MainActivity;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.net.NetComponent;
import com.wunu.smartlock1.net.api.LockEventApi;
import com.wunu.smartlock1.net.param.PB;
import com.wunu.smartlock1.net.response.LoginResult;
import com.wunu.smartlock1.net.response.ULoginResponse;
import com.wunu.smartlock1.service.AccountService;

import retrofit2.Call;


/**
 * 登录界面
 * Created by admin on 2018/4/21.
 */

public class LoginActivityA extends AppCompatActivity{

    Button login;
    EditText a;
    EditText a_1;
    TextView t;
    TextView t_b;
    Intent intent = new Intent();
    private ProgressDialog pd;
    PB pb = new PB();
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            ULoginResponse rs = (ULoginResponse)msg.obj;
            System.out.println("msg----------------------->:"+rs);

            if(rs == null||rs.getCode()!=200){
                Toast.makeText(LoginActivityA.this,"登录失败",Toast.LENGTH_SHORT).show();
                pd.dismiss();
                return;
            }

            Toast.makeText(LoginActivityA.this,"登录成功",Toast.LENGTH_SHORT).show();
            //只要执行到这里就关闭对话框
            pd.dismiss();
            intent.putExtra("param",new Gson().toJson(pb));
            startActivity(intent.setClass(LoginActivityA.this, LoginActivity.class));
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_lock_login_b);
        login = findViewById(R.id.btn_login);
        a = findViewById(R.id.et_account);
        a_1 = findViewById(R.id.cc);
        t = findViewById(R.id.checkBox_password);
        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent.setClass(LoginActivityA.this, LoginActivityX.class));
            }
        });
        t_b=findViewById(R.id.checkBox_login);
        t_b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent.setClass(LoginActivityA.this, LoginActivityV.class));
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread thread = new Thread(){
                    @Override
                    public void run(){
                        try {
                            System.out.println("执行登录前:---------------");
                            System.out.println("1: "+a.getText());
                            System.out.println("2: "+a_1.getText());
                            pb.setAccount(a.getText().toString());//"18320956072"
                            pb.setPassword(a_1.getText().toString());//"123123"
                            LockEventApi service = NetComponent.UserRetrofit.create(LockEventApi.class);
                            Call<ULoginResponse> call = service.loginA(pb);

                            ULoginResponse result = call.execute().body();


                            System.out.println("登录返回结果:"+result);

                            // 发送消息，把服务器返回的本文弹出吐司显示
                            Message msg = handler.obtainMessage();
                            msg.obj = result;
                            handler.sendMessage(msg);
                        }catch (Exception e){
                            System.out.println("登录异常信息:"+e);
                            Toast.makeText(LoginActivityA.this,"登录异常:"+e.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                };
                //构建一个下载进度条
                pd= ProgressDialog.show(LoginActivityA.this, "请等待", "正在登陆…");
                thread.start();
            }
        });
    }


}
